<?php

namespace Modules\Edu\Entities;

use App\Models\Traits\Favorite;
use App\Models\Traits\Favour;
use App\Models\Traits\Site;
use Illuminate\Database\Eloquent\Model;
use Modules\Edu\Entities\Traits\Comment;
use Spatie\Activitylog\Traits\LogsActivity;


class Topic extends Model
{
    use Site, Comment, Favour, Favorite, LogsActivity;

    protected $table = 'edu_topic';
    protected $fillable = ['title', 'content'];
    //LogsActivity options
    protected static $logName = 'topic';
    protected static $recordEvents = ['created'];

    public function tags()
    {
        return $this->morphToMany(Tag::class, 'relation', 'edu_tag_relation');

    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function getHtmlAttribute()
    {
        $Parsedown = new \Parsedown();
//        dd($Parsedown->text($this->content));
        return clean($Parsedown->text($this->content));
    }

    public function scopeSearch($query, $w = null)
    {
        if ($w) {
            return $query->where('title', 'like', "%{$w}%");
        }
    }

    public function getLinkAttribute()
    {
        return route('edu.front.topic.show', $this);
    }

}
